<script type="text/javascript">
   
    function delAlterationItem(btn){
		$(btn).closest("ul").remove();
	}
    
	function initFields(){
		initRadioOrCheckBoxValues();
		bindEvents();
		_toggleApplingType();
		initAlterationItems();
		init_toolTips();
		
	}
	
	function initRadioOrCheckBoxValues(){
		$("#copyrightAlterationForm input:radio[name='copyrightAlteration.originalRegType']").each(function(){
			var value = '<s:property value="copyrightAlteration.originalRegType"/>';
			if($(this).val() == value){
				$(this).attr('checked',true);
				return;
			}
		});
		
		$("#copyrightAlterationForm input:radio[name='copyrightAlteration.applyingType']").each(function(){
			var value = '<s:property value="copyrightAlteration.applyingType"/>';
			if($(this).val() == value){
				$(this).attr('checked',true);
				return;
			}
		});
	}
	
	function bindEvents(){
		$("#copyrightAlterationForm :input[name='copyrightAlteration.applyingType']").bind("click",function(){
			_toggleApplingType();
		});
	}
	
	function _toggleApplingType(){
		
		var checkedApplingTypeId = $("#copyrightAlterationForm :input[name='copyrightAlteration.applyingType']:checked").attr("id");
		
		if(checkedApplingTypeId == "copyrightAlteration-applyingType-0"){
			$("#supplyContainer :input").each(function(){
				$(this).attr("disabled",true);
			});
			
			$("#alterationContainer :input").each(function(){
				$(this).removeAttr("disabled");
			});
			
			$("#alterationContainer").show();
			$("#supplyContainer").hide();
		}else{
			$("#supplyContainer :input").each(function(){
				$(this).removeAttr("disabled");
			});
			
			$("#alterationContainer :input").each(function(){
				$(this).attr("disabled",true);
			});
			
			$("#alterationContainer").hide();
			$("#supplyContainer").show();
		}
		
	}
	
	
	function initAlterationItems(){
		$("#alterationContainer ul:first button.btn-danger").hide();
		$("#alterationContainer ul:not(:first) button.btn-danger").show();
		$("#alterationContainer ul:not(:first) button.btn-info").hide();
				
		$("#copyrightAlterationForm :input").bind('click focus',function(){
			$("#copyrightAlterationForm ul.highlighted").removeClass("highlighted");
			var current_li = $(this).closest("ul").not('.matrix').not('.buttons');
			current_li.addClass("highlighted");
		});
				
				
		$("#alterationContainer ul:first button.btn-info").bind('click',function(){
			$("#alterationContainer").append( '<ul class="item">' + $("#alterationContainer ul:first").html() + '</ul>');
			
			$("#alterationContainer ul:not(:first) button.btn-info").hide();
			$("#alterationContainer ul:not(:first) button.btn-danger").show();
			$("#alterationContainer ul:not(:first).highlighted").removeClass("highlighted");
				
		});
	}
	
	
	//check fields 
	function preSubmit(){
		var checkedApplingTypeId = $("#copyrightAlterationForm :input[name='copyrightAlteration.applyingType']:checked").attr("id");
		var items = "";
		var beforeContent = "";
		var afterContent = "";
		
		if(checkedApplingTypeId == "copyrightAlteration-applyingType-0"){
			$("#alterationContainer :input[name='alterationTypes']").each(function(){
				items += (items == "" ? "" : ";;;;") + $(this).val();
			});
			$("#alterationContainer :input[name='originalContents']").each(function(){
				beforeContent += (beforeContent == "" ? "" : ";;;;") + $(this).val();
			});;
			$("#alterationContainer :input[name='alteredContents']").each(function(){
				afterContent += (afterContent == "" ? "" : ";;;;") + $(this).val();
			});
		}else{
			items = $("#supplyContainer :input[name='alterationTypes']").val();
			beforeContent = $("#supplyContainer :input[name='originalContents']").val();
			afterContent = $("#supplyContainer :input[name='alteredContents']").val();
		}
		
		$("#copyrightAlterationForm input:hidden[name='copyrightAlteration.items']").val(items);
		$("#copyrightAlterationForm input:hidden[name='copyrightAlteration.beforeContent']").val(beforeContent);
		$("#copyrightAlterationForm input:hidden[name='copyrightAlteration.afterContent']").val(afterContent);
		
		
		return true;
		
	}
	
	
	
	function init_toolTips(){
		var tips = [{
			name:'copyrightAlteration.softFullName',msg:'<fmt:message key="tip.copyrightAlteration.softFullName" />'
		},{
			id:'copyrightAlteration-originalRegType-0',msg:'<fmt:message key="tip.copyrightAlteration.originalRegType.0" />'
		},{
			id:'copyrightAlteration-originalRegType-1',msg:'<fmt:message key="tip.copyrightAlteration.originalRegType.1" />'
		},{
			id:'copyrightAlteration-originalRegType-2',msg:'<fmt:message key="tip.copyrightAlteration.originalRegType.2" />'
		},{
			name:'copyrightAlteration.originalRegNumber',msg:'<fmt:message key="tip.copyrightAlteration.originalRegNumber" />'
		},{
			name:'copyrightAlteration.originalChangeNumber',msg:'<fmt:message key="tip.copyrightAlteration.originalChangeNumber" />'
		},{
			id:'copyrightAlteration-applyingType-0',msg:'<fmt:message key="tip.copyrightAlteration.applyingType.0" />'
		},{
			id:'copyrightAlteration-applyingType-1',msg:'<fmt:message key="tip.copyrightAlteration.applyingType.1" />'
		},{
			name:'alterationTypes',msg:'<fmt:message key="tip.copyrightAlteration.alterationType" />'
		},{
			name:'originalContents',msg:'<fmt:message key="tip.copyrightAlteration.originalContent" />'
		},{
			name:'alteredContents',msg:'<fmt:message key="tip.copyrightAlteration.alteredContent" />'
		},{
			name:'copyrightAlteration.reason',msg:'<fmt:message key="tip.copyrightAlteration.reason" />'
		},{
			name:'copyrightAlteration.sqrname',msg:'<fmt:message key="tip.copyrightAlteration.sqrname" />'
		},{
			name:'copyrightAlteration.sqraddress',msg:'<fmt:message key="tip.copyrightAlteration.sqraddress" />'
		},{
			name:'copyrightAlteration.sqrpostCode',msg:'<fmt:message key="tip.copyrightAlteration.sqrpostCode" />'
		},{
			name:'copyrightAlteration.sqrcontactPerson',msg:'<fmt:message key="tip.copyrightAlteration.sqrcontactPerson" />'
		},{
			name:'copyrightAlteration.sqrphone',msg:'<fmt:message key="tip.copyrightAlteration.sqrphone" />'
		},{
			name:'copyrightAlteration.sqremail',msg:'<fmt:message key="tip.copyrightAlteration.sqremail" />'
		},{
			name:'copyrightAlteration.sqrmobile',msg:'<fmt:message key="tip.copyrightAlteration.sqrmobile" />'
		},{
			name:'copyrightAlteration.sqrfax',msg:'<fmt:message key="tip.copyrightAlteration.sqrfax" />'
		}];
		
		/*$("input[name='user.email']").popover({
				title : '<fmt:message key="tip.title" />',
				trigger : 'focus',
				content : '<fmt:message key="signup.tip.email" />'
			});*/
		
		var title = '<fmt:message key="tip.title" />';
		
		for(var i=0; i < tips.length;i++){
			_bindPopover(tips[i].id,tips[i].name,tips[i].msg,title);
		}
		
	}
	
</script>
